package com.jianshuge.app.adapter;

import java.util.ArrayList;
import java.util.List;

import com.jianshuge.app.AppContext;
import com.jianshuge.app.AppException;
import com.jianshuge.app.R;

import com.jianshuge.app.bean.Behavior;
import com.jianshuge.app.bean.MyResult;
import com.jianshuge.app.bean.MyBooklistDetail.MyBooklistItem;
import com.jianshuge.app.bean.MyBooklistDetail.Reply;
import com.jianshuge.app.common.BitmapManager;
import com.jianshuge.app.common.StringUtils;
import com.jianshuge.app.common.UIHelper;

import com.jianshuge.app.ui.BooklistDetail;
import com.jianshuge.app.widget.CustomTextView;
import com.jianshuge.app.widget.LinkView;
import com.jianshuge.app.widget.CustomTextView.divText;
import com.jianshuge.app.widget.LinkView.OnLinkClickListener;

import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;


public class ListViewMyBooklistItemAdapter extends MyBaseAdapter {

	private BooklistDetail 					context;			//运行上下文
	private List<MyBooklistItem> 		listItems;			//数据集合
	private LayoutInflater 				listContainer;		//视图容器
	private int 						itemViewResource;	//自定义项视图源
	private BitmapManager 				bmpManager;
	private int 						cur_booklist_id;
	private int 						cur_booklistitem_id;
	
	static class ListItemView{								//自定义控件集合  
			public ImageView book_image;  
		    public TextView book_title;
		    public TextView book_rating;
		    public LinkView book_recomment;
		    public TextView book_contributor;
		    
		    public TextView book_edit_recom;
		    public TextView book_vote;
		    public TextView book_comment;
		    public TextView book_favorite;

		    public LinearLayout relies;
		    
		    public TextView client;
		    public TextView date; 
	 }  

	/**
	 * 实例化Adapter
	 * @param context
	 * @param data
	 * @param resource
	 */
	public ListViewMyBooklistItemAdapter(Context context, List<MyBooklistItem> data, int resource, int booklist_id) {
		this.context = (BooklistDetail)context;			
		this.listContainer = LayoutInflater.from(context);	//创建视图容器并设置上下文
		this.itemViewResource = resource;
		this.listItems = data;
		this.bmpManager = new BitmapManager(BitmapFactory.decodeResource(context.getResources(), R.drawable.default_book_pic));
		this.cur_booklist_id = booklist_id;

	}
	
	public int getCount() {
		return listItems.size();
	}

	public Object getItem(int arg0) {
		return null;
	}

	public long getItemId(int arg0) {
		return 0;
	}
	   
	/**
	 * ListView Item设置
	 */
	public View getView(int position, View convertView, ViewGroup parent) {

		//自定义视图
		ListItemView  listItemView = null;
		
		if (convertView == null) {
			//获取list_item布局文件的视图
			convertView = listContainer.inflate(this.itemViewResource, null);
			
			listItemView = new ListItemView();
			//获取控件对象
			listItemView.book_image 		= (ImageView)convertView.findViewById(R.id.bookitem_listitem_book_image);
			listItemView.book_title 		= (TextView)convertView.findViewById(R.id.bookitem_listitem_book_title);
			listItemView.book_rating 	    = (TextView)convertView.findViewById(R.id.bookitem_listitem_douban_rating);
			listItemView.book_recomment 	= (LinkView)convertView.findViewById(R.id.bookitem_listitem_recomment);
			listItemView.book_edit_recom = (TextView)convertView.findViewById(R.id.bookitem_listitem_edit_recom);
			listItemView.book_contributor 	= (TextView)convertView.findViewById(R.id.bookitem_listitem_contributor);
		    
			
			listItemView.book_vote = (TextView)convertView.findViewById(R.id.bookitem_listitem_vote);
			listItemView.book_comment = (TextView)convertView.findViewById(R.id.bookitem_listitem_comment);
			listItemView.book_favorite = (TextView)convertView.findViewById(R.id.bookitem_listitem_favorite);
			
			listItemView.relies = (LinearLayout)convertView.findViewById(R.id.booklistitem_listitem_relies);
		    
			listItemView.date= (TextView)convertView.findViewById(R.id.comment_listitem_date);
			listItemView.client= (TextView)convertView.findViewById(R.id.comment_listitem_client);
			
			//设置控件集到convertView
			convertView.setTag(listItemView);
		}else {
			listItemView = (ListItemView)convertView.getTag();
		}
			
		MyBooklistItem my_booklist_item = listItems.get(position);
		
	    AppContext ac = (AppContext)context.getApplicationContext();
		final int cur_login_uid = ac.getLoginUid();
		if( my_booklist_item.user_id == cur_login_uid ) {
			listItemView.book_edit_recom.setVisibility(View.VISIBLE);
			listItemView.book_edit_recom.setTag(my_booklist_item);
			listItemView.book_edit_recom.setOnClickListener(editrecomClickListener);
		}

		//设置文字和图片
		String book_image_url = my_booklist_item.book_image_url;
		if(book_image_url.endsWith("portrait.gif") || StringUtils.isEmpty(book_image_url)){
			listItemView.book_image.setImageResource(R.drawable.default_book_pic);
		}else{
			bmpManager.loadBitmap(book_image_url, listItemView.book_image);
		}	
		listItemView.book_image.setOnClickListener(imageClickListener);
		listItemView.book_image.setTag(my_booklist_item);

		listItemView.book_title.setText(my_booklist_item.book_title);
		listItemView.book_rating.setText("豆瓣评分: " + my_booklist_item.book_rating);
		listItemView.book_recomment.setText("“ " + my_booklist_item.user_comment + " 。”");
		listItemView.book_contributor.setText("—— 来自 " + my_booklist_item.user_name);
		
		listItemView.book_favorite.setText("收藏");
		listItemView.book_favorite.setOnClickListener(favoriteClickListener);
		listItemView.book_favorite.setTag(my_booklist_item);

		listItemView.book_comment.setText("评论" + "(" + my_booklist_item.comments_count + ")");
		listItemView.book_comment.setTag(my_booklist_item);
		listItemView.book_comment.setOnClickListener(commentClickListener);
		
		listItemView.book_vote.setText("赞" + "(" + my_booklist_item.vote_up_count + ")" );
		listItemView.book_vote.setOnClickListener(voteClickListener);
		listItemView.book_vote.setTag(my_booklist_item);

		
		listItemView.relies.setVisibility(View.GONE);
		listItemView.relies.removeAllViews();//先清空
		if(my_booklist_item.replies.size() > 0){
			//评论数目
			
			View view = listContainer.inflate(R.layout.comment_reply, null);
			TextView tv = (TextView)view.findViewById(R.id.comment_reply_content);
			tv.setText(context.getString(R.string.comment_reply_title, my_booklist_item.replies.size()));
			listItemView.relies.addView(view);
			
			//评论内容
			for(Reply reply : my_booklist_item.replies ){
				View view2 = listContainer.inflate(R.layout.comment_reply, null);
				TextView tv2 = (TextView)view2.findViewById(R.id.comment_reply_content);
				tv2.setText(reply.user_name+"("+StringUtils.friendly_time(reply.create_time)+")："+reply.content);
				
				if( my_booklist_item.user_id == cur_login_uid ) {			
					TextView tv3 = (TextView)view2.findViewById(R.id.comment_reply_delete);
					tv3.setVisibility(View.VISIBLE);			
					tv3.setTag(reply);
					tv3.setOnClickListener(removeCommentClickListener);
					tv3.setText("删除");
				}
				
				listItemView.relies.addView(view2);
			}
			listItemView.relies.setVisibility(View.VISIBLE);
		}
		
		switch(my_booklist_item.getAppClient())
		{
			default:
				listItemView.client.setText("");
				break;
					
			case MyBooklistItem.CLIENT_MOBILE:
				listItemView.client.setText("来自:手机");
				break;
			case MyBooklistItem.CLIENT_ANDROID:
				listItemView.client.setText("来自:Android");
				break;
			case MyBooklistItem.CLIENT_IPHONE:
				listItemView.client.setText("来自:iPhone");
				break;
			case MyBooklistItem.CLIENT_WINDOWS_PHONE:
				listItemView.client.setText("来自:Windows Phone");
				break;
			
		}
		
		if(StringUtils.isEmpty(listItemView.client.getText().toString()))
			listItemView.client.setVisibility(View.GONE);
		else
			listItemView.client.setVisibility(View.VISIBLE);
		
		listItemView.date.setText(StringUtils.friendly_time(my_booklist_item.update_time));
		
		return convertView;
	}
	
	private View.OnClickListener imageClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			MyBooklistItem my_booklist_item = (MyBooklistItem)v.getTag();
			UIHelper.showBookDetail(v.getContext(), my_booklist_item.book_id);		
		}
	};

	private View.OnClickListener voteClickListener = new View.OnClickListener(){

		public void onClick(View v) {
			
			final MyBooklistItem my_booklist_item = (MyBooklistItem)v.getTag();
			if ( my_booklist_item == null ) {
				return;
			}

			context.voteBook(my_booklist_item);

		}
	};
	
	private View.OnClickListener favoriteClickListener = new View.OnClickListener(){
		
		public void onClick(View v) {
			
			final MyBooklistItem my_booklist_item = (MyBooklistItem)v.getTag();
			if ( my_booklist_item == null ) {
				return;
			}
			context.favoriteBook(my_booklist_item);
		}
	};
	
	
	private View.OnClickListener commentClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			MyBooklistItem my_booklist_item = (MyBooklistItem)v.getTag();
			context.startPubComment(my_booklist_item.id);
		}
	};
	
	private View.OnClickListener removeCommentClickListener = new View.OnClickListener(){
		public void onClick(View v) {
			Reply reply = (Reply)v.getTag();
			context.removeComment(reply.user_id, reply.id, reply.item_id);
		}
	};
	
	private View.OnClickListener editrecomClickListener = new View.OnClickListener(){
		public void onClick(final View v) {
			MyBooklistItem my_booklist_item = (MyBooklistItem)v.getTag();			
			UIHelper.showEditBookResult(
					(Activity)v.getContext(), 
					my_booklist_item.user_id, 
					cur_booklist_id, 
					my_booklist_item.book_id, 
					my_booklist_item.user_comment, 
					my_booklist_item.subject_id
			);
			
		}
	};
	
	
	private View.OnClickListener linkViewClickListener = new View.OnClickListener() {
		public void onClick(View v) {
			if(!isLinkViewClick()){
				UIHelper.ToastMessage(v.getContext(), "HELLO JIANSHUGE");
			}
			setLinkViewClick(false);
		}
	};
	
	private OnLinkClickListener linkClickListener = new OnLinkClickListener() {
		public void onLinkClick() {
			setLinkViewClick(true);
		}
	};

}

